#ifndef _CONNECTION_H_
#define _CONNECTION_H_

#include "protocol.h"

#include <time.h>
#include <cstring>
#include <list>
#include <string>
#include <map>
#include <pthread.h>

class Connection {

public:

	Connection (std::string peer_ip);
	~Connection ();

	static void* Thread (void* Ptr);

	bool IsPendingOutput ();
	const std::list<MessageHeader*>& LockOutputQueue ();
	void UnlockOutputQueue ();

	void InputMessage (MessageHeader* Msg);

	std::string GetPeerIP ();

private:

	void PostMessage (MessageHeader*);

	std::list<MessageHeader*> input_queue, output_queue;
	pthread_mutex_t input_mutex, output_mutex;

	bool is_logged, is_versioned;
	std::string peer_ip;
	
	time_t accept_time, last_activity;

	std::string peer_login;
	static std::map<std::string, Connection*> login_map;
	pthread_mutex_t login_map_mutex;

	pthread_t thread_id;

	bool running;

};

#endif
